package com.quanyan.orderpay.web;

import com.quanyan.alipay.util.HttpRequest;
import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.enums.BaseEnum;
import com.quanyan.common.exception.BizException;
import com.quanyan.orderpay.request.ReqOrderInfo;
import com.quanyan.orderpay.response.RespOrderInfo;
import com.quanyan.orderpay.service.OrderPayService;
import com.quanyan.orderpay.service.check.account.SaveAccountWaterService;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wmb on 2016/9/12.
 */
@RestController
@RequestMapping("/api/pay")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderPayService orderPayService;

    @Autowired
    private SaveAccountWaterService saveAccountWaterService;

    @RequestMapping(value="/orderQuery",method = RequestMethod.POST)
    public APIResponse<RespOrderInfo> orderQuery(@RequestBody ReqOrderInfo reqOrderInfo,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        reqOrderInfo.setUid((Integer)request.getAttribute("uid"));
        APIRequest<ReqOrderInfo> apiRequest=new APIRequest<ReqOrderInfo>();
        apiRequest.setData(reqOrderInfo);
        return orderPayService.orderQuery(apiRequest);
    }

    @RequestMapping(value="/health")
    public APIResponse<BaseEnum> health(){
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value="/orderQueryBatch",method = RequestMethod.POST)
    public APIResponse<RespOrderInfo> orderQueryBatch(@RequestBody ReqOrderInfo reqOrderInfo,HttpServletRequest request){
        return null;
    }

    @RequestMapping(value = "/checkAccount/download", method = RequestMethod.GET)
    public APIResponse<BaseEnum> accountWater() {
        DateTime end = new DateTime().minusDays(1);

        DateTime begin = end.minusMonths(3);
        if (begin.compareTo(new DateTime("2017-03-01")) < 0) {
            begin = new DateTime("2017-03-01");
        }

        for (DateTime cur = begin; cur.compareTo(end) <= 0; cur = cur.plusDays(1)) {
            try {
                saveAccountWaterService.saveAlipayAccountWater(cur.toString("yyyy-MM-dd"));
                saveAccountWaterService.saveWechatAccountWater(cur.toString("yyyyMMdd"));

                logger.info("account water saved - {}", cur);
            } catch (Exception e) {
                logger.error(e.toString());
            }
        }

        return APIResponse.returnSuccess();
    }
    

    @RequestMapping(value="/checkAccount/savedate")
    public APIResponse<BaseEnum> saveAccountWaterbyDate(HttpServletRequest request){
        String strDate = request.getParameter("date");
        DateTime date = new DateTime(strDate);

        saveAccountWaterService.saveAlipayAccountWater(date.toString("yyyy-MM-dd"));
        saveAccountWaterService.saveWechatAccountWater(date.toString("yyyyMMdd"));

        return APIResponse.returnSuccess();
    }
}
